package test.kit.hutool.zip;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

@Slf4j
public class ZipHelper {

    @Test
    public void test(InputStream zip) {
        File tmpFile = new File("");
        FileUtil.writeFromStream(zip, tmpFile);
        try (ZipFile zipFile = new ZipFile(tmpFile, ZipFile.OPEN_READ, Charset.forName("GBK"))) {
            //
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                //
                ZipEntry entry = entries.nextElement();
                String name = entry.getName();
                if (entry.isDirectory()) {
                    log.info(">>>>> 忽略目录 {}", name);
                    continue;
                }
                InputStream item = zipFile.getInputStream(entry);
                log.info("文件 {}, {}", name, entry.getSize());
            }
        } catch (Exception ex) {
            throw new RuntimeException("", ex);
        }
    }
}
